<script setup>
  import { onLoad } from '@dcloudio/uni-app'
  import { getTaskDetailApi } from '@/apis/task.js'
  import { ref } from 'vue'

  const taskDetail = ref({})

  // 获取地址参数
  onLoad((params) => {
    getTaskDetail(params.id)
  })

  const getTaskDetail = async (id) => {
    const { code, data } = await getTaskDetailApi(id)
    if (code !== 200) return uni.utils.toast()
    taskDetail.value = data
  }
</script>

<template>
  <view class="page-container">
    <view class="search-bar">
      <!-- 在h5端显示搜索图标, 其它端显示扫码 -->
      <!-- #ifndef H5 -->
      <text class="iconfont icon-scan"></text>
      <!-- #endif -->
      <!-- #ifdef H5 -->
      <text class="iconfont icon-search"></text>
      <!-- #endif -->
      <input class="input" type="text" placeholder="输入运单号" />
    </view>
    <scroll-view scroll-y class="task-detail">
      <view class="scroll-view-wrapper">
        <view class="basic-info panel">
          <view class="panel-title">基本信息</view>
          <view class="timeline">
            <view class="line">{{ taskDetail.startAddress }}</view>
            <view class="line">{{ taskDetail.endAddress }}</view>
            <navigator
              hover-class="none"
              url="/subpkg_task/guide/index"
              class="guide"
            >
              <text class="iconfont icon-guide"></text>
              <text>开始导航</text>
            </navigator>
          </view>
          <view class="info-list">
            <view class="info-list-item">
              <text class="label">任务编号</text>
              <text class="value">{{ taskDetail.transportTaskId }}</text>
            </view>
            <view class="info-list-item">
              <text class="label">提货联系人</text>
              <text class="value">{{ taskDetail.startHandoverName }}</text>
            </view>
            <view class="info-list-item">
              <text class="label">联系电话</text>
              <text class="value">{{ taskDetail.startHandoverPhone }}</text>
            </view>
            <view class="info-list-item">
              <text class="label">预计提货时间</text>
              <text class="value">{{ taskDetail.planDepartureTime }}</text>
            </view>
            <!-- 非待提货展示 -->
            <view class="info-list-item" v-if="taskDetail.status !== 1">
              <text class="label">实际提货时间</text>
              <text class="value">{{ taskDetail.actualDepartureTime }}</text>
            </view>

            <view class="hr"></view>

            <view class="info-list-item">
              <text class="label">交付联系人</text>
              <text class="value">{{ taskDetail.finishHandoverName }}</text>
            </view>
            <view class="info-list-item">
              <text class="label">联系电话</text>
              <text class="value">{{ taskDetail.finishHandoverPhone }}</text>
            </view>
            <view class="info-list-item">
              <text class="label">预计送达时间</text>
              <text class="value">{{ taskDetail.planArrivalTime }}</text>
            </view>
            <!-- 已交付和已完成展示 -->
            <view
              class="info-list-item"
              v-if="taskDetail.status === 4 || taskDetail.status === 6"
            >
              <text class="label">实际送达时间</text>
              <text class="value">{{ taskDetail.actualArrivalTime }}</text>
            </view>
          </view>
        </view>

        <!-- 异常信息 -->
        <view class="except-info panel" v-if="taskDetail.exceptionList">
          <view class="panel-title">异常信息</view>
          <view
            class="info-list"
            v-for="(item, index) in taskDetail.exceptionList"
            :key="index"
          >
            <view class="info-list-item">
              <text class="label">上报时间</text>
              <text class="value">{{ item.exceptionTime }}</text>
            </view>
            <view class="info-list-item">
              <text class="label">异常类型</text>
              <text class="value">{{ item.exceptionType }}</text>
            </view>
            <view class="info-list-item">
              <text class="label">处理结果</text>
              <text class="value">{{ item.handleResult }}</text>
            </view>
          </view>
        </view>

        <!-- 提货信息 -->
        <view class="panel pickup-info" v-if="taskDetail.status >= 2">
          <view class="panel-title">提货信息</view>
          <view class="label">提货凭证</view>
          <view class="pictures">
            <block v-if="taskDetail.cargoPickUpPictureList.length">
              <image
                v-for="(obj, index) in taskDetail.cargoPickUpPictureList"
                :key="index"
                class="picture"
                :src="obj.url"
                mode="aspectFill"
              ></image>
            </block>
            <view v-else class="picture-blank">暂无图片</view>
          </view>
          <view class="label">货品照片</view>
          <view class="pictures">
            <block v-if="taskDetail.cargoPictureList.length">
              <image
                v-for="(obj, index) in taskDetail.cargoPictureList"
                :key="index"
                class="picture"
                :src="obj.url"
                mode="aspectFill"
              ></image>
            </block>
            <view v-else class="picture-blank">暂无图片</view>
          </view>
        </view>

        <!-- 交货信息 -->
        <view
          class="delivery-info panel"
          v-if="taskDetail.status === 4 || taskDetail.status === 6"
        >
          <view class="panel-title">交货信息</view>
          <view class="label">交货凭证</view>
          <view class="pictures">
            <block v-if="taskDetail.certificatePictureList">
              <image
                v-for="(item, index) in taskDetail.certificatePictureList"
                :key="index"
                class="picture"
                :src="item.url"
                mode="aspectFill"
              ></image>
            </block>
            <view v-else class="picture-blank">暂无图片</view>
          </view>
          <view class="label">货品照片</view>
          <view class="pictures">
            <block v-if="taskDetail.deliverPictureList">
              <image
                v-for="(item, index) in taskDetail.deliverPictureList"
                :key="index"
                class="picture"
                :src="item.url"
                mode="aspectFill"
              ></image>
            </block>
            <view v-else class="picture-blank">暂无图片</view>
          </view>
        </view>
      </view>
    </scroll-view>

    <!-- 待提货对应的操作为【延迟提货】和【提货】 -->
    <view class="toolbar" v-if="taskDetail.status === 1">
      <navigator
        v-if="taskDetail.enablePickUp"
        :url="`/subpkg_task/delay/index?id=${taskDetail.id}&planDepartureTime=${taskDetail.planDepartureTime}`"
        hover-class="none"
        class="button secondary"
        >延迟提货</navigator
      >
      <button v-else disabled class="button">延迟提货</button>
      <navigator
        v-if="taskDetail.enablePickUp"
        :url="`/subpkg_task/pickup/index?id=${taskDetail.id}`"
        hover-class="none"
        class="button primary"
        >提货</navigator
      >
      <button v-else disabled class="button primary">提货</button>
    </view>
    <!-- 在途对应的操作为【异常上报】和【交付】 -->
    <view class="toolbar" v-if="taskDetail.status === 2">
      <navigator
        :url="`/subpkg_task/except/index?transportTaskId=${taskDetail.transportTaskId}`"
        hover-class="none"
        class="button secondary"
        >异常上报
      </navigator>
      <navigator
        :url="`/subpkg_task/delivery/index?id=${taskDetail.id}`"
        hover-class="none"
        class="button primary"
        >交付</navigator
      >
    </view>
    <!-- 已交付对应的操作为【回车登记】 -->
    <view class="toolbar" v-if="taskDetail.status === 4">
      <navigator
        :url="`/subpkg_task/record/index?transportTaskId=${taskDetail.transportTaskId}&actualDepartureTime=${taskDetail.actualDepartureTime}`"
        hover-class="none"
        class="button primary block"
        >回车登记</navigator
      >
    </view>
  </view>
</template>

<style lang="scss" scoped>
  @import './index.scss';
</style>
